{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--BOOK_INFORMATION-->\n",
    "<img align=\"left\" style=\"padding-right:10px;\" src=\"figures/PHydro-cover-small.png\">\n",
    "*This is the Jupyter notebook version of the [Python in Hydrology](http://www.greenteapress.com/pythonhydro/pythonhydro.html) by Sat Kumar Tomer.*\n",
    "*Source code is available at [code.google.com](https://code.google.com/archive/p/python-in-hydrology/source).*\n",
    "\n",
    "*The book is available under the [GNU Free Documentation License](http://www.gnu.org/copyleft/fdl.html). If you have comments, corrections or suggestions, please send email to satkumartomer@gmail.com.*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--NAVIGATION-->\n",
    "< [Useful Attributes and Methods](03.02-Useful-Attributes-and-Methods.ipynb) | [Contents](Index.ipynb) | [Array Manipulation](03.04-Array-Manipulation.ipynb) >"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.3 索引\n",
    "\n",
    "在本节中，我们将讨论如何引用`numpy`数组中的一些元素。请记住，Python中的第一个索引为0。我们会生成一些数组，例如某个数组序列[0，1，...,9]的元素的3次方。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  0,   1,   8,  27,  64, 125, 216, 343, 512, 729])"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "foo = np.arange(10)**3\n",
    "foo"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "打印数组中的第三项。第三项意味着我们需要把索引设为2。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "foo[2]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "假设，我们想要打印一些数组序列，例如2,3和4的索引。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 8, 27, 64])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "foo[2:5]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们使用`2:5`获取2,3和4的索引值。这与`foo[np.arrange(2,5,1)]`相同。当我们没有在数组的索引中指定第三个值，默认情况下取1。如果我们要打印2到8的数值，间距为3。现在因为间距不是1，所以我们需要来定义它。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  8, 125, 512])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "foo[2:10:3]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果我们将索引中的第一个条目留空，即从以间距为2和最大到6的数组开始处获取数组元素，我们发出以下指令:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  8, 64])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "foo[:6:2] #给出在0,2,4的元素"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "我们获得元素达到了索引4，因为`arange`没有达到第二个参数。我们也可以使用索引修改数组中现有的元素，如同我们访问它们一样。让我们用-1000取代在索引0,2和4位置现有的元素。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1000,     1, -1000,    27, -1000,   125,   216,   343,   512,   729])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "foo[:6:2] = -1000 #修改在0,2,4的元素\n",
    "foo"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们通过索引-1得到数组最后一个元素。我们也可以使用这个，通过增加-1来反转数组。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们可以一次对整个numpy数组进行计算。假设我们有兴趣估计numpy数组的平方根，我们可以使用numpy库的`sqrt`函数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\laihetao\\AppData\\Local\\conda\\conda\\envs\\analysis\\lib\\site-packages\\ipykernel\\__main__.py:1: RuntimeWarning: invalid value encountered in sqrt\n",
      "  if __name__ == '__main__':\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([         nan,   1.        ,          nan,   5.19615242,\n",
       "                nan,  11.18033989,  14.69693846,  18.52025918,\n",
       "        22.627417  ,  27.        ])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sqrt(foo) #计算开根号"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`nan`表示元素`不是数字`。所以当元素值为负时，输出结果`sqrt`变为`nan`。Python发出的警告的告诉我们，在输入中存在一些无效的值，`sqrt`不能产生任何合理的输出，并且发出了警告(不是错误)。实际上，负数的平方个是复数，但是因为我们没有定义变量为复数，所以numpy就不能够在这里对复数进行操作。在这种情况下，我们需要处理负数的库。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
